#pragma once
#include <vector>
#include "Command.h"
#include "Utility.h"
#include "UpdateProcess.h"
#include "RenderProcess.h"
#include "BattleGround.h"

namespace bs
{
	class Actor;
	class Menu;

	struct ActorCfg
	{
		int hp;
		int mp;
		int at;
		int df;
		int as;
		std::string imageFile;
	};

	class Scene : public ICommandProcessor,  public IUpdateable, public IRenderable
	{
		DECLARE_COMMAND_MAP()

	public:
		Scene(HWND wnd);
		~Scene();

		virtual void Update(size_t time, size_t frames, size_t elapse);
		virtual void Render(HDC dc);

		bool Init();

		HDC PrepareToDraw();      // return the back dc
		void FlushToScreen();

		Actor* GetCurActor() { return this->actionOrder[this->curActor]; }
		BattleGround& GetBattleGround() { return this->bg; }

	private:
		void LoadConfig();
		void AdvanceToNextState();
		void DoPlace();
		void DoFreeControl();
		void DoMove();
		void DoAttack();

		inline void AdjustWindowPos();
		inline void CreateDoubleBuffer();
		inline void DeleteDoubleBuffer();

		inline void OnLBtnStatus(const Command& cmd);
		inline void OnRBtnStatus(const Command& cmd);
		inline void OnLBtnClk(const Command& cmd);
		inline void OnCursorWinPos(const Command& cmd);

		void OnMenuMoveClick(const Command& cmd);
		void OnMenuAttackClick(const Command& cmd);
		void OnMenuRestClick(const Command& cmd);
		void OnAniBegin(const Command& cmd);
		void OnAniEnd(const Command& cmd);

		void OnActorMove(const Command& cmd);
		void OnActorAttack(const Command& cmd);

		enum State
		{
			WAIT_PLACE_SEL,
			FREE_CONTROL,
			WAIT_MOVE_SEL,
			WAIT_ATTACK_SEL,
		};

	private:
		BattleGround bg;

		int myHp;
		int otherHp;
		bool playerTurn;

		std::vector<ActorCfg> actorCfgs;

		State state;
		bool waitAnim;
 		std::vector<Point> moveables;
 		std::vector<Point> attackables;

		std::vector<Actor*> actionOrder;
		int curActor;

		bool lBtnClk;
		bool lBtnDown;
		bool rBtnDown;
		Point curPos;

		Menu* menu;

		RECT clientRt;

		HWND   gameWnd;
		HBRUSH backBr;
		HDC frontDc;
		HDC backDc;
		HBITMAP backBmp;
		HBITMAP oldBmp;

		friend class BattleLogic;
	};

}